package cn.iocoder.yudao.module.wine.controller.admin.supplymarket;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.supplymarket.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplymarket.SupplyMarketDO;
import cn.iocoder.yudao.module.wine.service.supplymarket.SupplyMarketService;

@Tag(name = "管理后台 - 酒类行情管理")
@RestController
@RequestMapping("/wine/supply-market")
@Validated
public class SupplyMarketController {

    @Resource
    private SupplyMarketService supplyMarketService;

    @PostMapping("/create")
    @Operation(summary = "创建酒类行情管理")
    @PreAuthorize("@ss.hasPermission('wine:supply-market:create')")
    public CommonResult<Integer> createSupplyMarket(@Valid @RequestBody SupplyMarketSaveReqVO createReqVO) {
        return success(supplyMarketService.createSupplyMarket(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新酒类行情管理")
    @PreAuthorize("@ss.hasPermission('wine:supply-market:update')")
    public CommonResult<Boolean> updateSupplyMarket(@Valid @RequestBody SupplyMarketSaveReqVO updateReqVO) {
        supplyMarketService.updateSupplyMarket(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除酒类行情管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:supply-market:delete')")
    public CommonResult<Boolean> deleteSupplyMarket(@RequestParam("id") Integer id) {
        supplyMarketService.deleteSupplyMarket(id);
        return success(true);
    }

    @DeleteMapping("/delete-by-day")
    @Operation(summary = "删除前几天的酒类行情信息")
    @Parameter(name = "day", description = "天使", required = true)
    @PreAuthorize("@ss.hasPermission('wine:supply-market:delete')")
    public CommonResult<Boolean> deleteSupplyMarketByDay(@RequestParam("day") Integer day) {
        SupplyMarketDelVO delVO = new SupplyMarketDelVO();
        delVO.setStartTime(LocalDateTime.now().minusDays(day));
        delVO.setEndTime(LocalDateTime.now().minusDays(day));
        supplyMarketService.deleteSupplyMarketList(delVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得酒类行情管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:supply-market:query')")
    public CommonResult<SupplyMarketRespVO> getSupplyMarket(@RequestParam("id") Integer id) {
        SupplyMarketDO supplyMarket = supplyMarketService.getSupplyMarket(id);
        return success(BeanUtils.toBean(supplyMarket, SupplyMarketRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得酒类行情管理分页")
    @PreAuthorize("@ss.hasPermission('wine:supply-market:query')")
    public CommonResult<PageResult<SupplyMarketRespVO>> getSupplyMarketPage(@Valid SupplyMarketPageReqVO pageReqVO) {
        PageResult<SupplyMarketDO> pageResult = supplyMarketService.getSupplyMarketPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplyMarketRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出酒类行情管理 Excel")
    @PreAuthorize("@ss.hasPermission('wine:supply-market:export')")
    @OperateLog(type = EXPORT)
    public void exportSupplyMarketExcel(@Valid SupplyMarketPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplyMarketDO> list = supplyMarketService.getSupplyMarketPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "酒类行情管理.xls", "数据", SupplyMarketRespVO.class,
                        BeanUtils.toBean(list, SupplyMarketRespVO.class));
    }

}